package com.kejiguan.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.kejiguan.ui.R;


/**
 * Created by YYY on 16/7/19.
 */
public class CommonOneDialog extends Dialog implements View.OnClickListener{

    private View mDialogView;
    private TextView commonText;
    private TextView centerBtn;
    private View closeBtn;

    public CommonOneDialog(Context context) {
        super(context);
        init(context);
    }

    public CommonOneDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected CommonOneDialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
        setCanceledOnTouchOutside(true);
    }

    private void init(Context context) {

        mDialogView = View.inflate(context, R.layout.dialog_comm_one_layout, null);
        commonText = (TextView) mDialogView.findViewById(R.id.common_text);
        centerBtn = (TextView) mDialogView.findViewById(R.id.dialog_one_btn);
        closeBtn=mDialogView.findViewById(R.id.close_btn);
        setContentView(mDialogView);
        setCanceledOnTouchOutside(true);
        closeBtn.setOnClickListener(this);
    }

    public CommonOneDialog withBtnTxt(String left)
    {
        centerBtn.setText(left);
        return this;
    }

    public CommonOneDialog withMessage(String message) {
        commonText.setText(TextUtils.isEmpty(message) ? "" : message);
        return this;
    }

    public CommonOneDialog withOnClick(View.OnClickListener onClick) {
        centerBtn.setOnClickListener(onClick);
        return this;
    }

    public CommonOneDialog withTag(int tag) {
        centerBtn.setTag(R.id.recyclerview_item_position, tag);;
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.close_btn:
                dismiss();
                break;
        }
    }
}
